<template>
  <div class="auxiliary">
    <page-header title="辅材列表">
      <page-search :config="searchConfig" :model="searchModel" @search="getData">
        <el-button @click="showDialog()">添加辅材</el-button>
      </page-search>
    </page-header>
    <el-table :data="tableData" :cell-class-name="setCellClassName" :max-height="clientHeight - 250">
      <el-table-column
        v-for="column in columns"
        :key="column.prop"
        :prop="column.prop"
        :min-width="column.minWidth"
        :label="column.label">
        <template slot-scope="{ row }">
          <el-avatar :size="42" :src="row.coverUrl" v-if="column.prop === 'coverUrl'"></el-avatar>
          <template v-else>{{row[column.prop]}}</template>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="80px">
        <template slot-scope="{ row }">
          <action-text @click="showDialog(row)">修改</action-text>
          <action-text @click="deleteData(row)" type="danger">删除</action-text>
        </template>
      </el-table-column>
    </el-table>
    <page-pagination
      :pageSize="pageSize"
      :currentPage="pageNum"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <formDialog ref="dialog" :initFormData="formData" :isCreate="isCreate" @confirm="getData"/>
  </div>
</template>
<script>
import paginationMixin from '@/mixins/pagination';
import { getSupplementList, deleteSupplement, getCourseList } from '@/api/course';
import formDialog from './dialog';

const initFormData = {
  name: '',
  coverUrl: '',
  price: undefined,
  salePrice: undefined,
  freightPrice: undefined,
  courseId: '',
};

export default {
  data() {
    return {
      searchConfig: [
        {
          type: 'input',
          label: '辅材名称',
          prop: 'name',
        },
        {
          type: 'select',
          label: '所属课程',
          prop: 'courseId',
          // remote: '',
          options: [],
        },
      ],
      searchModel: {
        name: '',
        courseId: '',
      },
      columns: [
        { prop: 'name', label: '辅材名称', minWidth: '100px' },
        { prop: 'courseName', label: '所属课程', minWidth: '120px' },
        { prop: 'coverUrl', label: '主图', minWidth: '100px' },
        { prop: 'salePrice', label: '售价', minWidth: '100px' },
        { prop: 'price', label: '原价', minWidth: '100px' },
        { prop: 'freightPrice', label: '运费', minWidth: '100px' },
        { prop: 'createTime', label: '创建时间', minWidth: '100px' },
      ],
      tableData: [],
      formData: {
        title: '',
        content: '',
      },
      isCreate: false,
    };
  },
  components: {
    formDialog,
  },
  mixins: [paginationMixin],
  methods: {
    viewDetail(row) {
      console.log(row);
    },
    setCellClassName({ column }) {
      if (column.property === 'coverUrl') {
        return 'image-cell';
      }
      return '';
    },
    async deleteData(row) {
      this.$confirm('确定删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        await deleteSupplement({
          courseMaterialId: row.id,
        });
        this.$message.success('删除成功');
        await this.getData();
      }).catch(() => {});
    },
    showDialog(data) {
      if (data) {
        const {
          id, coverUrl, name, courseId, price, salePrice, freightPrice, courseName,
        } = data;
        this.formData = {
          id, coverUrl, name, courseId, price, salePrice, freightPrice, courseName,
        };
        this.isCreate = false;
      } else {
        this.formData = { ...initFormData };
        this.isCreate = true;
      }
      this.$refs.dialog.dialogVisible = true;
    },
    async getData() {
      const result = await getSupplementList(this.searchParams);
      const { total, list } = result;
      this.total = total;
      this.tableData = list && list.map((item) => {
        const {
          createTime, price, salePrice, freightPrice,
        } = item;
        return {
          ...item,
          price: price / 100,
          salePrice: salePrice / 100,
          freightPrice: freightPrice / 100,
          createTime: this.$formatTime(createTime),
        };
      });
    },
    async getCourseList() {
      const { list } = await getCourseList({
        name: '',
        pageSize: 100,
        pageNum: 0,
        valid: 1,
      });
      this.searchConfig[1].options = list.map((item) => ({
        value: item.id,
        label: item.name,
      }));
    },
  },
  async created() {
    await this.getCourseList();
    await this.getData();
  },
};
</script>
